package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements IWechatService {
    private static final String OPENID_URL="https://api.weixin.qq.com/sns/jscode2session";
    private static final String TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token";
    private static final String PHONE_URL="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.secret}")
    private String secret;


    @Override
    public String getOpenId(String code) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid",appid);
        paramMap.put("secret",secret);
        paramMap.put("js_code",code);
        paramMap.put("grant_type","authorization_code");
        String result = HttpUtil.get(OPENID_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //怎么判断调用成功还是失败呢
        if(jsonObject.getInt("errcode")==null || jsonObject.getInt("errcode")==0){
            //成功
            String openid = jsonObject.getStr("openid");
            return openid;
        }else {
            throw new RuntimeException("调用微信API失败:"+jsonObject.toJSONString(1));
        }
    }

    @Override
    public String getPhone(String detailCode) {
        Map<String,Object> paramMap = new HashMap<>();
        String token=getToken();
        paramMap.put("code",detailCode);
        String result = HttpUtil.post(PHONE_URL + token, JSONUtil.toJsonStr(paramMap));
        //转成json对象方便取数据
        JSONObject jsonObject = JSONUtil.parseObj(result);
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        //怎么判断调用成功还是失败呢
        if(jsonObject.getInt("errcode")==null || jsonObject.getInt("errcode")==0){
            //成功
            String phoneNumber = phoneInfo.getStr("phoneNumber");
            return phoneNumber;
        }else {
            throw new RuntimeException("调用微信API失败:"+jsonObject.toJSONString(1));
        }
    }


    /**
     * 获取openid
     * @param
     * @return
     */
    public String getToken() {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid",appid);
        paramMap.put("secret",secret);
        paramMap.put("grant_type","client_credential");
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //怎么判断调用成功还是失败呢
        if(jsonObject.getInt("errcode")==null || jsonObject.getInt("errcode")==0){
            //成功
            String accessToken = jsonObject.getStr("access_token");
            return accessToken;
        }else{
            throw new RuntimeException("调用微信API失败:"+jsonObject.toJSONString(1));
        }
    }
}

